package com.pactera.mavenweb.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.pactera.mavenweb.dao.UserDao;
import com.pactera.mavenweb.service.UserDetailsService;

 

@Service("userDetailsService")
public class UserDetailsServiceImpl implements UserDetailsService {
	protected static Logger logger = Logger.getLogger(UserDetailsServiceImpl.class);
	/**
	 * 注入dao
	 */
//	@Resource(name ="userDetailDao")
//	private UserDetailDao userDetailDao;
	
	@Resource(name ="userDao")
	private UserDao userDao;
//	
//	
//	public UserDetailDao getUserDetailDao() {
//		return userDetailDao;
//	}
//
//
//
//
//	public void setUserDetailDao(UserDetailDao userDetailDao) {
//		this.userDetailDao = userDetailDao;
//	}




//	@Override
	public UserDetails loadUserByUsername(String username)
	
			throws UsernameNotFoundException, DataAccessException {
		
		// 根据用户名获取帐户和权限信息
		
//		String sql = "";
//		sql = "SELECT username,password,status,name rname"
//				
//							+ " FROM user u,role r,user_role ur"
//							
//							+ " WHERE u.id=ur.user_id AND r.id=ur.role_id AND username=?";
		
		// 如果一个用户具有多个权限，连接查询会返回一个List
		
//		String hql = "from UserDetail where username = ?";
//		String[] strArray = {username};
		
		com.pactera.mavenweb.entity.User userWithAuth = userDao.queryByNameWithAuth(username);
		
//		// 取出帐户和权限信息填充到User中返回
//		
		if (userWithAuth == null || !userWithAuth.isEnabled()) {
			throw new UsernameNotFoundException("用户不存在或无效！");
		}
//		
//		// 如果用户存在
//		
//		Map<String, Object> map = (Map<String, Object>) list.get(0);
//		
//		// 密码
//		
//		String password = (String) map.get("password");
//		
//		// 帐户是否可用
//		
//		boolean enabled = ((Integer) map.get("status") == 1);
//		
//		// 帐户所具有的权限
//		
//		Collection<GrantedAuthorityImpl> gas = new ArrayList<GrantedAuthorityImpl>(list.size());
//		
//		for (int i = 0; i < list.size(); i++) {
//			
//			Map<String, Object> temp = (Map<String, Object>) list.get(i);
//			GrantedAuthorityImpl ga = new GrantedAuthorityImpl((String) temp.get("rname"));
//			gas.add(ga);
//			

		userWithAuth.achieveAuthorities();
		
		Collection<GrantedAuthority> gas = null;
		GrantedAuthority ga = null;
		Set<String> authorities = userWithAuth.achieveAuthorities();
//		if(authorities.contains(",")) {
//		String[] authority = authorities.split(",");
		
		if(authorities != null && authorities.size() > 0) {
			gas = new ArrayList<GrantedAuthority>();
			
			for (String authority: authorities) {
				ga = new SimpleGrantedAuthority(authority);
				gas.add(ga);
			}
			
			User user = new User(username, userWithAuth.getPassword(), userWithAuth.isEnabled(), true, true, true, gas);
			
			if (logger.isDebugEnabled()) {
				logger.debug("user info = " + user.toString());
			} else {
				logger.info("logged in user info = " + user.toString());
				
			}
			
			return user;
		} else {
			throw new AccessDeniedException("Access denied");
		}
		
//		} else {
//			ga = new SimpleGrantedAuthority(authorities);
//			gas.add(ga);
//		}
//		System.out.println("userDetail.isEnabled() = " + userDetail.isEnabled());
//		
		// spring-security提供的类
		
		
		
	}
}
